package socketmvc.server.context;

import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import socketmvc.core.exception.ImToBlacklistContextOperateException;
import socketmvc.core.packets.basic.IPacket;

/**
 * 要列入黑名单上下文
 * 该对象只能用来调用黑名单相关的api
 * @author xueyanjun
 */
public class SocketToBlacklist extends SocketChannel {

    private final String clientIp;
    private final int clientPort;
    private final TioConfig ioConfig;

    public SocketToBlacklist(String clientIp, int clientPort, TioConfig ioConfig) {
        this.clientIp = clientIp;
        this.clientPort = clientPort;
        this.ioConfig = ioConfig;
    }

    public SocketToBlacklist(String clientIp, int clientPort, IServerContext serverContext) {
        this.clientIp = clientIp;
        this.clientPort = clientPort;
        this.ioConfig = serverContext.getIoConfig();
    }

    private SocketToBlacklist(ChannelContext target, String type) {
        super(target, type);
        this.clientIp = null;
        this.clientPort = 0;
        this.ioConfig = null;
    }

    /**
     * 获取im服务上下文
     *
     * @return context
     */
    @Override
    public IServerContext getServerContext() {
        throwBlacklistEx();
        return null;
    }

    @Override
    public IPacket getPacket() {
        throwBlacklistEx();
        return null;
    }

    @Override
    public Object getAttr(String key) {
        throwBlacklistEx();
        return null;
    }

    @Override
    public Object setAttr(String key, Object val) {
        throwBlacklistEx();
        return null;
    }

    @Override
    public Object removeAttr(String key) {
        throwBlacklistEx();
        return null;
    }

    @Override
    public String getType() {
        throwBlacklistEx();
        return null;
    }

    @Override
    public TioConfig getIoConfig() {
        return this.ioConfig;
    }

    @Override
    public void send(IPacket packet) {
        throwBlacklistEx();
    }

    @Override
    public boolean sendToUser(String user, IPacket packet) {
        throwBlacklistEx();
        return Boolean.FALSE;
    }

    @Override
    public void sendToGroup(String group, IPacket packet) {
        throwBlacklistEx();
    }

    @Override
    public void bindUser(String user) {
        throwBlacklistEx();
    }

    @Override
    public void bindGroup(String group) {
        throwBlacklistEx();
    }

    @Override
    public void unbindUser(String user) {
        throwBlacklistEx();
    }

    @Override
    public void unbindGroup(String group) {
        throwBlacklistEx();
    }

    @Override
    public boolean getUserIsOnline(String user) {
        throwBlacklistEx();
        return false;
    }

    @Override
    public void close() {
        throwBlacklistEx();
    }

    @Override
    public String getClientIp() {
        return this.clientIp;
    }

    @Override
    public int getClientPort() {
        return this.clientPort;
    }

    private void throwBlacklistEx(){
        throw new ImToBlacklistContextOperateException("context操作异常，ImToBlacklistContext只能用来调用设置黑名单相关的方法");
    }
}
